package com.yuyang.wifi.activitys.mine;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.yuyang.wifi.R;
import com.yuyang.wifi.activitys.login.ApkAutoUpgradeActivity;
import com.yuyang.wifi.activitys.web.WebViewSecretActivity;
import com.yuyang.wifi.base.BaseActivity;
import com.yuyang.wifi.base.BaseResult;
import com.yuyang.wifi.constant.AppConstant;
import com.yuyang.wifi.model.CheckUpgradeModel;
import com.yuyang.wifi.model.CheckUpgradeResult;
import com.yuyang.wifi.utils.DatePickerUtil;
import com.yuyang.wifi.utils.DeviceInfoUtils;
import com.yuyang.wifi.utils.RxUtil;
import com.yuyang.wifi.utils.Skip;
import com.yuyang.wifi.utils.SpUtil;
import com.yuyang.wifi.utils.ToastUtils;
import com.yuyang.wifi.utils.network.BaseDataSubscriber;
import com.yuyang.wifi.utils.network.HttpErrorHandler;
import com.yuyang.wifi.utils.network.HttpManager;
import com.yuyang.wifi.utils.network.RxDataInstance;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SettingActivity extends BaseActivity {

    @BindView(R.id.express_2_ad_container)
    FrameLayout express2AdContainer;
    @BindView(R.id.tv_version)
    TextView tvVersion;

    @Override
    protected void initActionBar() {


    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initContentView() {

        String registrationID = JPushInterface.getRegistrationID(SettingActivity.this);
        Log.e("TAG", "registrationID===: " + registrationID);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @OnClick({R.id.iv_back, R.id.rl_my_suggest_return, R.id.rl_privacy_policy, R.id.rl_user_policy, R.id.rl_check_update})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_my_suggest_return:
                Skip.toActivity(SettingActivity.this, SuggestReturnActivity.class);
                break;
            case R.id.rl_privacy_policy:
                String privacyProtocol = SpUtil.find(AppConstant.KEY_PRIVACY_PROTOCOL);
                Intent intent = new Intent(SettingActivity.this, WebViewSecretActivity.class);
                intent.putExtra("url", privacyProtocol);
                intent.putExtra("title", "隐私协议");
                startActivity(intent);
                break;
            case R.id.rl_user_policy:
                String userProtocol = SpUtil.find(AppConstant.KEY_USER_PROTOCOL);
                Intent intent2 = new Intent(SettingActivity.this, WebViewSecretActivity.class);
                intent2.putExtra("url", userProtocol);
                intent2.putExtra("title", "用户协议");
                startActivity(intent2);
                break;
            case R.id.rl_check_update:
                checkUpgrade();
                break;
        }
    }

    private void checkUpgrade() {
        RxDataInstance.getInstance().initMap(this);
        HttpManager.getInstance().getApi().checkUpgrade(RxDataInstance.getInstance().getParamMap())
                .compose(RxUtil.<CheckUpgradeResult>bindLifecycleAndApplySchedulers(this))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseDataSubscriber<CheckUpgradeResult>(new HttpErrorHandler() {
                    @Override
                    public void onHttpError(BaseResult result) {
                        ToastUtils.showShortToast(SettingActivity.this, result.getErrorMessage());
                    }

                    @Override
                    public void onHttpException(Throwable e) {
                        // dismissLoadingDialog();
                    }
                }) {
                    @Override
                    public void onDataNext(CheckUpgradeResult result) {
                        int resultCode = result.getResultCode();
                        if (0 == resultCode) {
                            CheckUpgradeModel data = result.getData();
                            if (null != data) {
                                int newCode = data.getVersioncode();
                                int localVersionCode = DeviceInfoUtils.getLocalVersionCode(SettingActivity.this);
                                if (newCode > Integer.valueOf(localVersionCode)) {
                                    Intent intent = new Intent(SettingActivity.this, ApkAutoUpgradeActivity.class);
                                    intent.putExtra("data", data);
                                    startActivity(intent);
                                } else {
                                    ToastUtils.showShortToast(SettingActivity.this, "当前已经是最新版本");
                                }
                            }
                        } else {
                            ToastUtils.showShortToast(SettingActivity.this, result.getErrorMessage());
                        }
                    }
                });
    }


}
